package com.zbkj.service.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zbkj.common.model.project.ProjectReportItem;
import com.zbkj.common.request.project.AdminReportRequest;
import com.zbkj.service.dao.project.ProjectReportItemMapper;
import com.zbkj.service.service.project.ProjectReportItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 项目报告估算结果明细项 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-07-09
 */
@Service
public class ProjectReportItemServiceImpl extends ServiceImpl<ProjectReportItemMapper, ProjectReportItem> implements ProjectReportItemService {

    @Override
    public List<ProjectReportItem> getItemsByProjectId(Long projectId) {
        if(Objects.isNull(projectId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<ProjectReportItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectReportItem::getDeleteFlag, 0)
                .eq(ProjectReportItem::getProjectId, projectId);
        return this.list(queryWrapper);
    }

    @Override
    public boolean delByProjectId(Long projectId) {
        if(Objects.isNull(projectId)) {
            return false;
        }
        LambdaUpdateWrapper<ProjectReportItem> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(ProjectReportItem::getDeleteFlag, 1)
                .eq(ProjectReportItem::getDeleteFlag, 0)
                .eq(ProjectReportItem::getProjectId, projectId);
        return this.update(updateWrapper);
    }

    @Override
    public void saveProcess(Long projectId, List<AdminReportRequest.AdminReportItemRequest> itemRequestList) {
        this.delByProjectId(projectId);
        List<ProjectReportItem> itemList = Lists.newArrayList();
        for (AdminReportRequest.AdminReportItemRequest itemRequest : itemRequestList) {
            ProjectReportItem item = new ProjectReportItem();
            BeanUtils.copyProperties(itemRequest, item);
            item.setProjectId(projectId);
            itemList.add(item);
        }
        if(CollectionUtils.isNotEmpty(itemList)) {
            this.saveBatch(itemList,itemList.size());
        }
    }
}
